package k8s_ui

import (
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"k8s-platform/service/k8s-ui"
	"net/http"
)

// 获取deployment列表，支持过滤、排序、分页
var DaemonSet daemonSet

type daemonSet struct{}

func (*daemonSet) GetdaemonSets(ctx *gin.Context) {
	params := new(struct {
		FilterName string `form:"filter_name"`
		Namespace  string `form:"namespace"`
		Page       int    `form:"page"`
		Limit      int    `form:"limit"`
		Pool       string `form:"pool"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(params.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}

	data, err := k8s_ui.DaemonSet.GetdaemonSets(params.FilterName, params.Namespace, params.Limit, params.Page)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取Deployment列表成功",
		"data": data,
		"pool": pool,
	})
}

// 获取DaemonSet详情
func (d *daemonSet) GetDaemonSetDetail(ctx *gin.Context) {
	params := new(struct {
		DaemonSetName string `form:"daemonset_name"`
		Namespace     string `form:"namespace"`
		Pool          string `form:"pool"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(params.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
			"pool": pool,
		})
		return
	}

	data, err := k8s_ui.DaemonSet.GetdaemonSetDetail(params.DaemonSetName, params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取Deployment详情成功",
		"data": data,
		"pool": pool,
	})
}

// 创建DaemonSet
func (d *daemonSet) CreateDaemonSet(ctx *gin.Context) {
	var (
		daemonCreate = new(k8s_ui.DaemonSetCreate)
		err          error
	)
	if err = ctx.ShouldBindJSON(daemonCreate); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	pool, err_pool := k8s_ui.K8s.IntPool(daemonCreate.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}

	if err = k8s_ui.DaemonSet.CreatedaemonSet(daemonCreate); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "创建DaemonSet成功",
		"data": nil,
		"pool": pool,
	})
}

// 删除DaemonSet
func (d *daemonSet) DeleteDaemonSett(ctx *gin.Context) {
	params := new(struct {
		DaemonSetName string `json:"daemonSet_name"`
		Namespace     string `json:"namespace"`
		Pool          string `json:"pool"`
	})
	//DELETE请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(params.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}

	err := k8s_ui.DaemonSet.DeletedaemonSet(params.DaemonSetName,
		params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "删除daemonSet成功",
		"data": nil,
		"pool": pool,
	})
}

// 重启DaemonSet
func (d *daemonSet) RestartDaemonSet(ctx *gin.Context) {
	params := new(struct {
		DaemonSetName string `json:"daemonset_name"`
		Namespace     string `json:"namespace"`
		Pool          string `json:"pool"`
	})
	//PUT请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(params.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}

	err := k8s_ui.DaemonSet.RestartdaemonSet(params.DaemonSetName,
		params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "重启daemonSet成功",
		"data": nil,
		"pool": pool,
	})
}

// 更新DaemonSet
func (d *daemonSet) UpdateDaemonSet(ctx *gin.Context) {
	params := new(struct {
		Namespace string `json:"namespace"`
		Content   string `json:"content"`
		Pool      string `json:"pool"`
	})
	//PUT请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(params.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}

	err := k8s_ui.DaemonSet.UpdatedaemonSet(params.Namespace, params.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "更新daemonSet成功",
		"data": nil,
		"pool": pool,
	})
}

// 获取每个namespace的DaemonSet数量
func (d *daemonSet) GetdaemonSetNumPerNp(ctx *gin.Context) {
	data, err := k8s_ui.DaemonSet.GetdaemonSetNumPerNp()
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取每个namespace的DaemonSet数量成功",
		"data": data,
	})
}
